.TH std::shared_ptr::operatorbool 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::shared_ptr::operatorbool \- std::shared_ptr::operatorbool

.SH Synopsis
   explicit operator bool() const noexcept;

   Checks if *this stores a non-null pointer, i.e. whether get() != nullptr.

.SH Parameters

   \fI(none)\fP

.SH Return value

   true if *this stores a pointer, false otherwise.

.SH Notes

   An empty shared_ptr (where use_count() == 0) may store a non-null pointer accessible
   by get(), e.g. if it were created using the aliasing constructor.

.SH Example


// Run this code

 #include <iostream>
 #include <memory>

 void report(std::shared_ptr<int> ptr)
 {
     if (ptr)
         std::cout << "*ptr=" << *ptr << "\\n";
     else
         std::cout << "ptr is not a valid pointer.\\n";
 }

 int main()
 {
     std::shared_ptr<int> ptr;
     report(ptr);

     ptr = std::make_shared<int>(7);
     report(ptr);
 }

.SH Output:

 ptr is not a valid pointer.
 *ptr=7

.SH See also

   get returns the stored pointer
       \fI(public member function)\fP
